﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{

    /// <summary>
    /// Provide a DateTime object for which the Year, Month, Day, and Hour values are all 0 indexed.  For the default DateTime object the Hour property is 0 indexed, while no other properties are.
    /// </summary>
    public class DateTimeArrayIndex
    {
        // 0 Indexed: Hour
        // 1 Indexed: Year, Month, Day
        private DateTime _startingValue;
        private DateTime _endDate;
        private DateTime _current;
        private bool _dateTraversalComplete = false;
        private Dictionary<int, int> _yearMap = new Dictionary<int, int>();

        /// <summary>
        /// Initialize with a Starting Value
        /// </summary>
        /// <param name="StartingValue"></param>
        public DateTimeArrayIndex(DateTime StartingValue, int EndYear)
        {
            _current = StartingValue;
            _startingValue = StartingValue;
            _endDate = new DateTime(EndYear + 1, 1, 1);
            _dateTraversalComplete = false;

            int _x = 0;

            for (int i = _current.Year; i <= EndYear; ++i)
            {
                _yearMap.Add(i, _x);
                ++_x;
            }
        }

        /// <summary>
        /// Reset the current DateTime to the initial Starting Value
        /// </summary>
        public void Reset()
        {
            _current = _startingValue;
            _dateTraversalComplete = false;
        }

        /// <summary>
        /// Have we reached the EndDate?
        /// </summary>
        public bool DateTraversalComplete
        {
            get
            {
                return _dateTraversalComplete;
            }
        }

        /// <summary>
        /// Add (value) hours to the current time
        /// </summary>
        /// <param name="value"></param>
        public void AddHours(double value)
        {
            _current = _current.AddHours(value);

            if (_current >= _endDate)
                _dateTraversalComplete = true;
        }

        public void SubtractHours(double value)
        {
            double _v = Math.Abs(value) * -1;

            _current.AddHours(_v);

        }

        /// <summary>
        /// Set Current time to a new value.
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <param name="Day"></param>
        public void SetDateTime(int Year, int Month, int Day)
        {
            _current = new DateTime(Year, Month, Day);

            if (_current < _endDate)
                _dateTraversalComplete = false;

        }

        /// <summary>
        /// Return the underlying DateTime object
        /// </summary>
        public DateTime DateObject
        {
            get
            {
                return _current;
            }
        }

        /// <summary>
        /// 0 Indexed Year value
        /// </summary>
        public int Year
        {
            get
            {
                return _yearMap[_current.Year];
            }
        }

        /// <summary>
        /// 0 Indexed Month value
        /// </summary>
        public int Month
        {
            get
            {
                return _current.Month - 1;
            }
        }

        /// <summary>
        /// 0 Indexed Day Value
        /// </summary>
        public int Day
        {
            get
            {
                return _current.Day - 1;
            }
        }

        /// <summary>
        /// 0 Indexed Hour value
        /// </summary>
        public int Hour
        {
            get
            {
                return _current.Hour;
            }

        }

        
    }
}
